'use strict';
// require('./dingtalk');
process.env.NODE_ENV !== 'production' && (process.env.DEBUG = 'viewdata*');
const debug = require('debug')('viewdata:app');
const Koa = require('koa');
const koaBody = require('koa-body');
const conditional = require('koa-conditional-get');
const cors = require('koa2-cors');
const config = require('./config');
const router = require('./router');
const socket = require('./socket');
require('./cron')
// const graphqlServer = require('./graphql');
// require('./kafka');
const app = new Koa();
app.use(
  cors({
    origin: function (ctx) {
      //   if (ctx.url === '/test') {
      return ctx.request.headers.origin; // 允许来自所有域名请求
      //   }
    },
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
    maxAge: 5,
    credentials: true,
    allowMethods: ['GET', 'POST'],
    allowHeaders: ['Content-Type', 'Authorization', 'Accept', 'X-Requested-With'],
  }),
);
app.use(koaBody());
app.use(conditional());
// graphqlServer(app);
app.use(router.routes()).use(router.allowedMethods());
app.on('error', (err) => {
  console.error(err.stack);
});

// app.listen(config.port, '0.0.0.0');
// debug(`Koa listening on port ${config.port}`);
// debug(`visit: http://localhost:${config.port}`);

const server = require('http').Server(app.callback());

socket(server);

server.listen(config.port, () => {
  console.log(`app run at : http://127.0.0.1:${config.port}`);
})
